package com.example.dangdangbackend.controller;

import com.example.dangdangbackend.DTO.*;
import com.example.dangdangbackend.VO.BookTableVO;
import com.example.dangdangbackend.VO.SelectBooksVO;
import com.example.dangdangbackend.common.CustomException;
import com.example.dangdangbackend.entity.Book;
import com.example.dangdangbackend.entity.Result;
import com.example.dangdangbackend.service.BookService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/book")
@Slf4j
public class BookController {

    @Autowired
    private BookService bookService;

    @PostMapping("/addBook")
    public Result<String> addBook(BookDTO book){
        log.info(book.toString());
        bookService.insertBook(book);
        return Result.success("插入书目成功");
    }

    /**
     * 分页搜寻操作
     * @param bookSearchDTO
     * @return
     */
    @PostMapping("/bookSearch")
    public Result<PageInfo<BookTableVO>> bookSearch(BookSearchDTO bookSearchDTO){
        log.info("查找的方式为:{}",bookSearchDTO);
        PageInfo<BookTableVO> pageInfo = bookService.searchBooksLike(bookSearchDTO);
        return Result.success(pageInfo);
    }

    @PostMapping("/selectById")
    public Result<BookDTO> selectById(int id){
        BookDTO bookDTO = bookService.selectById(id);
        if(bookDTO==null){
            return Result.error("删除失败");
        }
        return Result.success(bookDTO);
    }

    @PostMapping("/updateById")
    public Result<String> updateById(UpdateBookDTO updateBookDTO){
        log.info(updateBookDTO.toString());
        bookService.updateBookById(updateBookDTO);
        return Result.success("修改成功");
    }

    @PostMapping("/deleteIds")
    public Result<String> deleteIds(@RequestBody List<Integer> bookIds){
        bookIds.stream().forEach(System.out::println);
        try{
            bookService.deleteIds(bookIds);
        }catch (Exception e){
            throw  new CustomException("所选不能为空");
        }

        return Result.success("删除成功");
    }

    @PostMapping("/bookCategorySearch")
    public Result<PageInfo<BookTableVO>> bookCategorySearch(CategorySearchDTO categorySearchDTO){
        log.info("分类方式为{}",categorySearchDTO.toString());
        PageInfo<BookTableVO> pageInfo = bookService.searchBookByCategory(categorySearchDTO);

        return Result.success(pageInfo);
    }

    @PostMapping("/addBookSales")
    public Result addBookSales(@RequestBody  List<AddBookSaleDTO> addBookSaleDTOS){
        bookService.addBookSales(addBookSaleDTOS);

        addBookSaleDTOS.stream().forEach(System.out::println);
        return Result.success();
    }

    @PostMapping("/alertStatus")
    public Result alertStatus(int bookId){
        bookService.alertStatus(bookId);

        return Result.success();
    }

    @PostMapping("/getRecommendBooks")
    public Result<List<SelectBooksVO>> getRecommendBooks(){

        return Result.success(bookService.getRecommendBooks());
    }

    @PostMapping("/getSalesMost")
    public Result<List<SelectBooksVO>> getSalesMost(){

        return Result.success(bookService.selectSalesMost());
    }

    @PostMapping("/getRecentBooks")
    public Result<List<SelectBooksVO>> getRecentBooks() {
        return Result.success(bookService.selectRecentBooks());
    }

    @PostMapping("/selectNewBookAndSalesMost")
    public Result<List<SelectBooksVO>> selectNewBookAndSalesMost() {
        return Result.success(bookService.selectNewBookAndSalesMost());
    }

}
